//
//  LeaderboardScene.m
//  iFly
//
//  Created by User-23 on 12/8/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.

#import "LeaderboardScene.h"
#import "MainMenuScene.h"
#import "GameOverScene.h"
#import "CoreDataHelper.h"
#import "Player.h"
#import "Constants.h"

@interface LeaderboardScene ()

@property NSString* previousScene;
@property NSInteger playerScore;

@end

static float screenScale;

@implementation LeaderboardScene {
    SKLabelNode* backLabel;
}

-(instancetype) initWithSize:(CGSize)size andPreviousScene:(NSString*) previousScene andScore:(NSInteger)score {
    self = [super initWithSize:size];
    if (self) {
        screenScale=size.height/640;
        
        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"amazing clouds"]];
        background.position = CGPointZero;
        background.anchorPoint = CGPointZero;
        [self addChild:background];
        
        SKSpriteNode* leaderIron = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"leaderIronMan-copy.png"]];
        leaderIron.position = CGPointMake(self.size.width-retreatRightBorder*screenScale, self.size.height/leaderIronManHeightOffset);
        [leaderIron setScale:screenScale];
        [self addChild:leaderIron];
        
        NSString *normalFont = @"Futura-CondensedExtraBold";
        NSInteger fontSize = self.frame.size.height * 0.055;
        float margin = self.frame.size.height * MarginPercent * 2;
        
        backLabel = [Constants createLabelWithFont:labelFontName text:@"Back To Main" fontColor:[SKColor blueColor] fontSize:25];
        float backLabelMargin = self.frame.size.width * 0.03;
        backLabel.position = CGPointMake(self.frame.size.width - backLabel.frame.size.width / 2 - margin, backLabelMargin);
        [self addChild:backLabel];
        
        SKLabelNode *headerLabel = [Constants createLabelWithFont:normalFont text:@"Leaderboard" fontColor:[SKColor purpleColor] fontSize:fontSize * 1.2];
        headerLabel.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height - headerLabel.frame.size.height - margin);
        [self addChild:headerLabel];
        
        float previousY = headerLabel.position.y;
        
        
        NSMutableArray* players = [[CoreDataHelper sharedCoreDataHelper] getPlayersInDescendingOrder];
        for (int i = 0; i < [players count]; i++) {
            Player* current = players[i];
            NSLog(@"Player: %@ with score: %@",current.name, current.score);
            NSString *text = [NSString stringWithFormat:@"%i. %@ - %@", i + 1, current.name, current.score];
            SKLabelNode *playerLabel = [Constants createLabelWithFont:labelFontName text:text fontColor:[SKColor blackColor] fontSize:fontSize];
            playerLabel.zPosition = 1;
            playerLabel.position = CGPointMake(playerLabel.frame.size.width / 2 + margin, previousY - playerLabel.frame.size.height - margin);
            [self addChild:playerLabel];
            
            previousY = playerLabel.position.y;
        }
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node isEqual:backLabel]) {
        MainMenuScene *mainMenu = [MainMenuScene sceneWithSize:self.frame.size];
        SKTransition *transition = [SKTransition fadeWithDuration:1];
        [self.view presentScene:mainMenu transition:transition];
    }
}
@end
